package org.ss.psci.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.ss.psci.entity.PmMessageType;
import org.ss.psci.entity.PmProjectType;
import org.ss.psci.entity.*;
import org.ss.psci.util.ListRange;

public class ConfigService {
	private BaseDAO baseDAO;
	private List list;

	public ListRange getPmMessageTypes(Map condition) {
		int rows, start, limit;
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				list = baseDAO.getPmMessageTypeDAO().findAll();
			}
			if (list == null) {
				ListRange listRange = new ListRange();
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}
			List result = new ArrayList();
			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmMessageType(String temp, Map formMap) {
		PmMessageType pu = new PmMessageType();
		try {
			BeanUtils.populate(pu, formMap);
			PmMessageType p = getPmMessageType(temp);

			if (p != null) {
				p=baseDAO.getPmMessageTypeDAO().copyEntity(p, pu);
				baseDAO.getPmMessageTypeDAO().save(p);
			} else {
				baseDAO.getPmMessageTypeDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delPmMessageType(String temp) {
		try {
			PmMessageType p = getPmMessageType(temp);
			if (p != null) {
				baseDAO.getPmMessageTypeDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmMessageType getPmMessageType(String temp) {
		PmMessageType p = new PmMessageType();
		List uList = baseDAO.getPmMessageTypeDAO().findByTypeName(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmMessageType) uList.get(0);
			}
		}
		return p;
	}
	
	
	public ListRange getPmProjectTypes(Map condition) {
		int rows, start, limit;
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				list = baseDAO.getPmProjectTypeDAO().findAll();
			}
			if (list == null) {
				ListRange listRange = new ListRange();
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}
			List result = new ArrayList();
			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmProjectType(String temp, Map formMap) {
		PmProjectType pu = new PmProjectType();
		try {
			BeanUtils.populate(pu, formMap);
			PmProjectType p = getPmProjectType(temp);
			if (p != null) {
				p=baseDAO.getPmProjectTypeDAO().copyEntity(p, pu);
				baseDAO.getPmProjectTypeDAO().save(p);
			} else {
				baseDAO.getPmProjectTypeDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delPmProjectType(String temp) {
		try {
			PmProjectType p = getPmProjectType(temp);
			if (p != null) {
				baseDAO.getPmProjectTypeDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmProjectType getPmProjectType(String temp) {
		PmProjectType p = new PmProjectType();
		List uList = baseDAO.getPmProjectTypeDAO().findByTypeName(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmProjectType) uList.get(0);
			}
		}
		return p;
	}

	
	
	
	
	public ListRange getPmStorageTypes(Map condition) {
		int rows, start, limit;
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				list = baseDAO.getPmStorageTypeDAO().findAll();
			}
			if (list == null) {
				ListRange listRange = new ListRange();
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}
			List result = new ArrayList();
			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmStorageType(String temp, Map formMap) {
		PmStorageType pu = new PmStorageType();
		try {
			BeanUtils.populate(pu, formMap);
			PmStorageType p = getPmStorageType(temp);
			if (p != null) {
				p=baseDAO.getPmStorageTypeDAO().copyEntity(p, pu);
				baseDAO.getPmStorageTypeDAO().save(p);
			} else {
				baseDAO.getPmStorageTypeDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delPmStorageType(String temp) {
		try {
			PmStorageType p = getPmStorageType(temp);
			if (p != null) {
				baseDAO.getPmStorageTypeDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmStorageType getPmStorageType(String temp) {
		PmStorageType p = new PmStorageType();
		List uList = baseDAO.getPmStorageTypeDAO().findByTypeName(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmStorageType) uList.get(0);
			}
		}
		return p;
	}
	
	
	
	
	
	
	
	
	
	public ListRange getPmUnits(Map condition) {
		int rows, start, limit;
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				list = baseDAO.getPmUnitDAO().findAll();
			}
			if (list == null) {
				ListRange listRange = new ListRange();
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}
			List result = new ArrayList();
			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmUnit(String temp, Map formMap) {
		PmUnit pu = new PmUnit();
		try {
			BeanUtils.populate(pu, formMap);
			PmUnit p = getPmUnit(temp);
			if (p != null) {
				p=baseDAO.getPmUnitDAO().copyEntity(p, pu);
				
				baseDAO.getPmUnitDAO().save(p);
			} else {
				baseDAO.getPmUnitDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delPmUnit(String temp) {
		try {
			PmUnit p = getPmUnit(temp);
			if (p != null) {
				baseDAO.getPmUnitDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmUnit getPmUnit(String temp) {
		PmUnit p = new PmUnit();
		List uList = baseDAO.getPmUnitDAO().findByUnitName(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmUnit) uList.get(0);
			}
		}
		return p;
	}
	
	
	
	//*******************************************************************************
	
	public ListRange getPmDocTypes(Map condition) {
		int rows, start, limit;
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				list = baseDAO.getPmDocTypeDAO().findAll();
			}
			if (list == null) {
				ListRange listRange = new ListRange();
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}
			List result = new ArrayList();
			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void savePmDocType(String temp, Map formMap) {
		PmDocType pu = new PmDocType();
		try {
			BeanUtils.populate(pu, formMap);
			PmDocType p = getPmDocType(temp);
		
			if (p != null) {
				p=baseDAO.getPmDocTypeDAO().copyEntity(p, pu);				
				baseDAO.getPmDocTypeDAO().save(p);
			} else {
				baseDAO.getPmDocTypeDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delPmDocType(String temp) {
		try {
			PmDocType p = getPmDocType(temp);
			if (p != null) {
				baseDAO.getPmDocTypeDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmDocType getPmDocType(String temp) {
		PmDocType p = new PmDocType();
		List uList = baseDAO.getPmDocTypeDAO().findByDocType(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmDocType) uList.get(0);
			}
		}
		return p;
	}
	
	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
}
